package com.nice.nice201110.service;

import com.nice.nice201110.entity.*;
import com.nice.nice201110.mapper.*;
import com.nice.nice201110.utils.NiceTableEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class TstorageService {
    //获取入库dao对象
    @Autowired
    TStorageMapper tStorageMapper;

    //获取出账明细dao对象
    @Autowired
    TDeliveryDelMapper tDeliveryDelMapper;

    //获取资产的Dao
    @Autowired
    TBaseCapitalMapper tBaseCapitalMapper;

    //获取药品的Dao
    @Autowired
    TBasePictureMapper tBasePictureMapper;

    @Autowired
    TLogMapper tLogMapper;

    @Autowired
    private PlatformTransactionManager ransactionManager;

    private TLog tLog = new TLog();

    /** 查询数据 */
    public List<TStorage> getList(BaseVo baseVo){
        return tStorageMapper.selectByPrimaryKeys(baseVo);
    }
    /** 单一查询  */
    public TStorage getOne(UserVo userVo, Integer id){
        TStorage tStorage = tStorageMapper.selectByPrimaryKey(id);
        if(tStorage!=null){
            tLog.setDate(new Date());
            tLog.setType(3);  //操作类型（新增0，修改1，删除2,查询3）
            tLog.setDocid(tStorage.getId());
            tLog.setUsername(userVo.getName());
            tLog.setUserid(Integer.valueOf(userVo.getId()));
            tLog.setTtable(NiceTableEnum.form_storage);
            tLog.setRemark("查看入库信息:"+tStorage.toString());
            tLogMapper.insertSelective(tLog);
        }
        return tStorage;
    }
    /** 入库操作 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean inserttstorage(UserVo userVo,TStorage tStorage){
        try {
            int num = tStorageMapper.insertSelective(tStorage);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(0);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tStorage.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_storage);
                tLog.setRemark("新增入库信息:"+tStorage.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
            //根据入库数量更新资产
            BigDecimal StorageQuantity = tStorage.getStorageQuantity();  //获取入库数量
            int CapitalId = tStorage.getName();  //资产id 药品id
            String ttype = tStorage.getType();  //类型（资产0 /药品1）
            if("0".equals(ttype)){
                TBaseCapital tBaseCapital = tBaseCapitalMapper.selectByPrimaryKey(CapitalId);
                if(tBaseCapital!=null){
                    Integer StorageQuantityInt = StorageQuantity.intValue();  // 入库数量
                    Integer dNumber = tBaseCapital.getNumber();  //获取资产数量
                    Integer allNumber = StorageQuantityInt+dNumber;
                    tBaseCapital.setNumber(allNumber);
                    Integer isSertNum = tBaseCapitalMapper.updateByPrimaryKeySelective(tBaseCapital);
                    if(isSertNum<0){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return false;
                    }
                    String CapitalName = tBaseCapital.getName();
                    tLog.setDate(new Date());
                    tLog.setType(1);  //操作类型（新增0，修改1，删除2）
                    tLog.setDocid(tBaseCapital.getId());
                    tLog.setUsername(userVo.getName());
                    tLog.setUserid(Integer.valueOf(userVo.getId()));
                    tLog.setTtable(NiceTableEnum.form_base_capital);
                    tLog.setRemark("系统自动新增["+CapitalName+"]资产:"+StorageQuantity.toString());
                    tLogMapper.insertSelective(tLog);
                }else{
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return false;
                }
            }else if("1".equals(ttype)){
                TBasePicture tBasePicture = tBasePictureMapper.selectByPrimaryKey(CapitalId);
                if(tBasePicture!=null){
                    BigDecimal dNumber = tBasePicture.getNumber();
                    BigDecimal allNumber = dNumber.add(StorageQuantity);
                    tBasePicture.setNumber(allNumber);
                    Integer isSertNum = tBasePictureMapper.insertSelective(tBasePicture);
                    if(isSertNum<0){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return false;
                    }
                    String PictureName = tBasePicture.getName();
                    tLog.setDate(new Date());
                    tLog.setType(1);  //操作类型（新增0，修改1，删除2）
                    tLog.setDocid(tBasePicture.getId());
                    tLog.setUsername(userVo.getName());
                    tLog.setUserid(Integer.valueOf(userVo.getId()));
                    tLog.setTtable(NiceTableEnum.form_base_picture);
                    tLog.setRemark("系统自动新增["+PictureName+"]药品:"+StorageQuantity.toString());
                    tLogMapper.insertSelective(tLog);
                }else{
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return false;
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 入库操作删除 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deletetstorage(UserVo userVo,Integer id){
        try {
            TStorage tStorage = tStorageMapper.selectByPrimaryKey(id);
            if(tStorage==null){return false;}
            int num = tStorageMapper.deleteByPrimaryKey(id);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(id);
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_storage);
                tLog.setRemark("删除入库信息:"+tStorage.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
            //根据入库数量更新资产
            BigDecimal StorageQuantity = tStorage.getStorageQuantity();  //获取入库数量
            int CapitalId = tStorage.getName();  //资产id 药品id
            String ttype = tStorage.getType();  //类型（资产0 /药品1）
            if("0".equals(ttype)){
                TBaseCapital tBaseCapital = tBaseCapitalMapper.selectByPrimaryKey(CapitalId);
                if(tBaseCapital!=null){
                    Integer StorageQuantityInt = StorageQuantity.intValue();  // 入库数量
                    Integer dNumber = tBaseCapital.getNumber();  //获取资产数量
                    Integer allNumber = dNumber - StorageQuantityInt;
                    tBaseCapital.setNumber(allNumber);
                    Integer isSertNum = tBaseCapitalMapper.updateByPrimaryKeySelective(tBaseCapital);
                    if(isSertNum<0){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return false;
                    }
                    String CapitalName = tBaseCapital.getName();
                    tLog.setDate(new Date());
                    tLog.setType(1);  //操作类型（新增0，修改1，删除2）
                    tLog.setDocid(tBaseCapital.getId());
                    tLog.setUsername(userVo.getName());
                    tLog.setUserid(Integer.valueOf(userVo.getId()));
                    tLog.setTtable(NiceTableEnum.form_base_capital);
                    tLog.setRemark("系统自动回滚["+CapitalName+"]资产:"+StorageQuantity.toString());
                    tLogMapper.insertSelective(tLog);
                }else{
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return false;
                }
            }else if("1".equals(ttype)){
                TBasePicture tBasePicture = tBasePictureMapper.selectByPrimaryKey(CapitalId);
                if(tBasePicture!=null){
                    BigDecimal dNumber = tBasePicture.getNumber();
                    BigDecimal allNumber = dNumber.subtract(StorageQuantity);
                    tBasePicture.setNumber(allNumber);
                    Integer isSertNum = tBasePictureMapper.insertSelective(tBasePicture);
                    if(isSertNum<0){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return false;
                    }
                    String PictureName = tBasePicture.getName();
                    tLog.setDate(new Date());
                    tLog.setType(1);  //操作类型（新增0，修改1，删除2）
                    tLog.setDocid(id);
                    tLog.setUsername(userVo.getName());
                    tLog.setUserid(Integer.valueOf(userVo.getId()));
                    tLog.setTtable(NiceTableEnum.form_base_picture);
                    tLog.setRemark("系统自动回滚["+PictureName+"]药品:"+StorageQuantity.toString());
                    tLogMapper.insertSelective(tLog);
                }else{
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return false;
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
}
